package com.lungle.warmdiary.app.view.common;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;

import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.base.BaseActivity;
import com.lungle.warmdiary.app.base.BaseFragment;
import com.lungle.warmdiary.app.view.active.fragment.ActiveDetailFragment;
import com.lungle.warmdiary.app.view.message.fragment.MessageDetailFragment;
import com.lungle.warmdiary.app.view.wallet.fragment.TradeDetailFragment;
import com.lungle.warmdiary.app.view.wallet.fragment.WithdrawDetailFragment;

import java.lang.ref.WeakReference;


public class DetailActivity extends BaseActivity {

    public static final int DISPLAY_NEWS = 0;
    public static final int DISPLAY_ACTIVE = 3;
    public static final int DISPLAY_MESSAGE = 7;
    public static final int DISPLAY_TRADE = 4;
    public static final int DISPLAY_WITHDRAW = 5;
    public static final String BUNDLE_KEY_DISPLAY_TYPE = "BUNDLE_KEY_DISPLAY_TYPE";
    // private static final String DETAIL_SCREEN = "detail_screen";

    private WeakReference<BaseFragment> mFragment, mEmojiFragment;

    @Override
    protected int getActionBarTitle() {
        return R.string.actionbar_title_detail;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_detail;
    }


    @Override
    protected boolean hasBackButton() {
        return true;
    }


    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        int displayType = getIntent().getIntExtra(BUNDLE_KEY_DISPLAY_TYPE,
                DISPLAY_NEWS);
        BaseFragment fragment = null;
        int actionBarTitle = 0;
        switch (displayType) {
            case DISPLAY_ACTIVE:
                actionBarTitle = R.string.actionbar_title_active;
                fragment = new ActiveDetailFragment();
                break;
            case DISPLAY_MESSAGE:
                actionBarTitle = R.string.actionbar_title_message;
                fragment = new MessageDetailFragment();
                break;
            case DISPLAY_TRADE:
                actionBarTitle = R.string.actionbar_title_trade_detail;
                fragment = new TradeDetailFragment();
                break;
            case DISPLAY_WITHDRAW:
                actionBarTitle = R.string.actionbar_title_withdraw_detail;
                fragment = new WithdrawDetailFragment();
                break;
            default:
                break;
        }
        setActionBarTitle(actionBarTitle);

        FragmentTransaction trans = getSupportFragmentManager()
                .beginTransaction();
        mFragment = new WeakReference<>(fragment);
        trans.replace(R.id.container, fragment);
//        if (fragment instanceof ToolbarFragmentControl) {
//            ToolbarFragment f = new ToolbarFragment();
//            // mEmojiFragment = new WeakReference<BaseFragment>(f);
//            trans.replace(R.id.toolbar_container, f);
//            ((ToolbarFragmentControl) fragment).setToolBarFragment(f);
//        }

//        trans.commit();
        trans.commitAllowingStateLoss();
    }


    @Override
    public void onBackPressed() {

        if (mFragment != null && mFragment.get() != null) {
            if (mFragment.get().onBackPressed()) {
                return;
            }
        }
        super.onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // MobclickAgent.onPageStart(DETAIL_SCREEN);
        // MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // MobclickAgent.onPageEnd(DETAIL_SCREEN);
        // MobclickAgent.onPause(this);
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }
}
